package net.sparrow.module;

import com.jcraft.jogg.Packet;

import net.minecraft.src.Packet28EntityVelocity;
import net.sparrow.Sparrow;
import net.sparrow.cmd.CommandHandler;
import net.sparrow.event.DataField;
import net.sparrow.event.SpEventListener;
import net.sparrow.event.SpPacketProcessEvent;
import net.sparrow.util.Util;

public class KnockbackModule extends Module {

	
	//@DataField( id="player.noknockback")
	//public Boolean noKnockback = true;
	
	@DataField( id="player.knockmod")
	public Double knockMult = .2;
	
	@DataField( id="player.knockboost")
	public Boolean boost = false;
	
	//TODO knockbackBoost feature
	@SpEventListener
	public void onPacket(SpPacketProcessEvent e) {
		//Util.print("Got packet: "  + e.packet.getClass());
		if(e.packet instanceof Packet28EntityVelocity) {
			Packet28EntityVelocity packet = ((Packet28EntityVelocity)e.packet);
			if(packet.entityId == Sparrow.mc().player().entityId) {
				if(boost) {
					Util.debug("Booster");
					Sparrow.mc().player().motionX *= 1.2;
					Sparrow.mc().player().motionY *= 1.2;
					Sparrow.mc().player().motionZ *= 1.2;
				}
				if(Sparrow.mc().player().hurtResistantTime == 0) {
					return; //this velocity packet is not due to knockback,
					//its some serverside (boost block, double jmp etc)
				}
				
				if(knockMult == 0) {
					packet.entityId = -1; //shouldnt need this 
					return;
				}
				
				//entity-velocity is set aboslutely, but knockback is 
				//the CHANGE in the player's movement
				Double dX = packet.motionX - Sparrow.mc().player().motionX;
				Double dY = packet.motionY - Sparrow.mc().player().motionY;
				Double dZ = packet.motionZ - Sparrow.mc().player().motionZ;

				dX *= knockMult;
				dY *= knockMult;
				dZ *= knockMult;
				//so modify the change in velocity by the knockback multiplier
				//ex. if knockmult = 0, there will be no change
				//values less than 0, or more than 1, are very strange
				
				//jesus christ minecraft, sending a value that is a double as an int from the server
				//are you serious
				packet.motionX = (int) (Sparrow.mc().player().motionX + dX);
				packet.motionY = (int) (Sparrow.mc().player().motionY + dY);
				packet.motionZ = (int) (Sparrow.mc().player().motionZ + dZ);
			}
		}
	}
	
	public CommandHandler loadCommands() {
		return new CommandHandler().withDefaultToggle("player.knockmod").withDefaultToggle("player.knockboost");
	}
}
